var newsTable;
var liveid = document.getElementById("liveid").value;
var liveids = document.getElementById("liveids").value;

//获取广告信息
function getRelatedVideo(mainid,liveid) {
    if (typeof newsTable !== 'undefined') {
        newsTable.fnPageChange('first');
        return;
    }
    var i = 1;
    newsTable = $("#newsList").DataTable({
        "sAjaxSource": _path + "/relatedVideo/queryByPage",
        "fnServerParams": function (aoData) {
            aoData.push(
                { 
                	"name": "videoName",
                	"value": $("#videoName").val() 
                },{
					"name" : "name",
					"value" : $("#liveName").val()
				},{
					"name" : "mainid",
					"value" : mainid
				},{
					"name" : "startTime",
					"value" : $("#startTime").val()
				}, {
					"name" : "endTime",
					"value" : $("#endTime").val()
				}
            );
        },
        "sServerMethod": "POST",
        "sAjaxDataProp": "dataList",
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
	        { "sTitle": "&#8194<input type='checkbox' id='SelectAll' onclick='selectAll();'>", "sWidth": "1%",
	            "mData": function (data) {
	              	var status = '<a href="' + _path + '/relatedVideo/edit/' + data.id + '"></a>';
	              	var strs= new Array(); //定义一数组 
	              	strs=liveids.split(","); //字符分割 
	            	status += '&nbsp;<input type="checkbox"';
	              	for (a=0;a<strs.length ;a++ ) 
	              	{ 
	              	if(data.id==strs[a]){
	              		status += 'checked="true"';
	              	}
	              	} 
	              	status += 'id="ck"  name="ck"  onchange="myfunction('+data.id+')" value="'+data.id+'">';	
	              	    //status +='&nbsp;<a href="javascript:deletevideo('+ data.id+ ');"  class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';
	                    return status;
	            }
	        },
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            }/*,
            { "sTitle": "视频名称", "sWidth": "10%" ,"mData": 
	            	function(data){
	        		//var temps = '<a href="' + _path + '/relatedVideo/edit/' + data.id + '" >'+data.videoName+'</a>';
	        		var temps = '<a>'+data.videoName+'</a>';
	        		return temps;
	        	}
	        }*/,
	        { "sTitle": "直播间名称", "mData": "name", "sWidth": "10%" },
	        { "sTitle": "视频图片", "mData": 'background',  "sWidth": '10%', 
            	"mData": function (data) {
            		var status = '<img style="height:100px;width:160px" src="'+data.background+'"/>';
                    return status;
            }	
            },
            //{ "sTitle": "视频地址", "mData": "videoUrl", "sWidth": "10%" },
//            { "sTitle": "观看次数", "mData": "watchNumber", "sWidth": "10%" },
            { "sTitle": "发布时间", "mData": "createTime", "sWidth": "10%"}
        ]
    });
}
//var numbers=0;
//function myfunction(id){
//	if(numbers==0){
//		numbers++;
//		layer.alert("此操作是用来删除或是把勾选的数据在前台页面展现给用户的一个操作(注:这只是一个弹框提示,并且只会弹出一次的哦。)", {
//	        icon: 6,
//	        offset: 'middle',
//	        //time: 10000, 
//	        //closeBtn : [0 , true],
//	        title : ['友情提示' , true], //控制标题栏。'信息'：标题栏文字，true：是否显示标题栏（否：false）
//	        skin: 'layui-layer-lan'
//	    });
//	}
//}

//function deletevideo() {
//	var videoId = "";
//	var ckValues = document.getElementsByName("ck");   
//	for(var i=0;i<ckValues.length;i++){
//		if(ckValues[i].checked==true){
//			videoId = videoId+ckValues[i].value+";";
//		}
//	}
////	alert(videoId)
//	if(videoId==null||videoId==""){
//		layer.open({
//			  title: '警告！'
//			  ,content: '<center>请至少选中一条数据！</center>'
//			}); 
//		return;
//	}
//	
//	layer.confirm('您确定要删除吗？', {
//		btn : [ '确定', '取消' ]
//	// 按钮
//	}, function() {
//		location.href = _path + "/relatedVideo/del?videoId=" + videoId;
//	}, function() {
//
//	});
//}

//function check(){
//	layer.confirm('确定选择完毕', function(){
//		    save();
//		}, function(){
//			
//		});
//}

function save(){
//获取当前所选中的	
var videoId = "";
//未被选中的id
var noId="";
var ckValues = document.getElementsByName("ck");   
for(var i=0;i<ckValues.length;i++){
	if(ckValues[i].checked==true){
		videoId = videoId+ckValues[i].value+",";
	}
//	else{
//		noId = noId+ckValues[i].value+",";
//	}
}
if(videoId==null||videoId==""){
	layer.open({
		  title: '警告！'
		  ,content: '<center>请至少选中一条数据！</center>'
		}); 
	return;
}else{
	$.ajax({
		cache : true,
        type: "POST",
        url: _path + "/relatedVideo/save?videoId="+videoId+"&liveid="+liveid,
        async : false,
		dataType : "json",
        error: function (request) {
        },
        success: function (data) {
            if (data.ok) {
            	layer.msg('保存成功！');
            	setTimeout("Refresh()",1000);
            }else {
            	layer.msg('保存失败！');
            	setTimeout("Refresh()",1000);
			}
        },
    });
}
}